library(foreign)
library(dummies)

data <- read.csv("finalcoding.csv", skip = 1)

data2 <- data

#Change 96, 97, 98, 99 to NA
for (i in 1:ncol(data2)){

data2[,i] <- as.numeric(as.character(data2[,i]))
data2[,i] <- ifelse(data2[,i] %in% c(97, 98, 99), NA, data2[,i])
print(i)		

}

#drop unanimous columns before dummying them out - then do this again at the end.
sds <- apply(data2, 2, function(x) sd(x))
cols <- names(sds[sds == 0])
data2 <- data2[,names(data2) %in% cols == FALSE]

drop <- c("name", "source", "length", "preambw", "rightwrds", "regions", "denomum", "hosname", "uhname", "ordname", "conterm", "ecterm", "langoffw", "langnatw")

data3 <- data2[,!(colnames(data2) %in% drop)]

not.dummies <- c("uhterm", "doctit", "docs", "hosterm", "lhseats", "uhseats", "agterm", "chfterm", "adterm", "ordterm", "demonum", "model", "rightsw", "colrule", "prevlead", "forinves", "amndapct", "execnum", "hoshog", "hoselect", "hoselsys", "hosterml", "hosage", "hossucc", "hosimm", "hosstaff", "hosdecim", "hosdecex", "hogname", "hogelect", "hogterml", "hogage", "hoglegr", "hogsucc", "hogimm", "hogdecm", "hogdecex", "depsel", "cabcoll", "cabage", "cabrestl", "atgen", "agterml", "agage", "emdecl", "housenum", "legjoint", "lhlead", "lhcohort", "lhelsys", "lhage", "lhterm", "lhtrmlim", "uhlead", "uhcohort", "uhelsys", "uhage", "uhtrmlim", "legdiss", "legrep", "immunity", "intexec", "legsupr", "legapp", "legappdf", "legapppt", "overwho", "overpct", "attend", "profleg", "recvote", "levjud", "chftrmnm", "chfage", "supterm", "suptermn", "supage", "ordterml", "ordage", "adtermn", "adage", "conlim", "conage", "chalstag", "jrem", "judretir", "fedunit", "fedsep", "seccess", "partprf", "partprh", "votemin", "oversght", "ecterml", "ecage", "electsch", "electsam", "census", "bankgoal", "custlaw2", "headforn", "warap", "treatst", "excrim", "cappun", "corppun", "examwit", "nat", "resenex", "infoaccw", "offrel", "offrelw", "jointrde", "proprght", "safework", "childwrk", "marriage", "slave", "torture", "censor", "press", "nomil", "govmed", "lang", "langprot", "edcompl", "edfreel", "achighed")

dummies <- colnames(data3)[!(colnames(data3) %in% not.dummies)]
#dummies <- dummies[!(dummies %in% names(data3)[1])]

for (i in 1:length(dummies)){
	
name <- dummies[i]	
	
#change 2's to 0's	
data3[,colnames(data3) == name] <- ifelse(data3[,colnames(data3) == name] == 2, 0, data3[,colnames(data3) == name])
	
#change NA's to 0's	
data3[,colnames(data3) == name] <- ifelse(is.na(data3[,colnames(data3) == name]), 0, data3[,colnames(data3) == name])

#create a dummy variable
table(data3[,colnames(data3) == name], useNA = "always")
x <- dummy(data3[,colnames(data3) == name])

if (ncol(x) == 1){
  if (colnames(x) == "data30") {
  x <- cbind(x, 0)
  } else if (colnames(x) == "data31") {
  x <- cbind(0, x)
} } else {
  print("no")
}

if (ncol(x) == 2){
#take the second column of the dummy variable - the "yes" vote
data3[,colnames(data3) == name] <- x[,2]
print(name)	
table(data3[,colnames(data3) == name], useNA = "always")
print(name) } else if (ncol(x) == 3) {
  data3 <- data.frame(data3, x[,c(1,2)])
  names(data3)[c(ncol(data3)-1,ncol(data3))] <- c(paste(name, 0, sep = ""), paste(name, 1, sep = ""))
  data3[,colnames(data3) == name] <- NULL  
print(name) }

x <- as.data.frame(x)

}

not.dummies2 <- not.dummies[not.dummies %in% colnames(data3)]

################################
################################
################################
	
#change NA's to 0's	
data3$adterm[is.na(data3$adterm)] <- 0
	
table(data3$adterm, useNA = "always")
data3$adterm <- ifelse(data3$adterm > 0, 1, 0)

################################

#change NA's to 0's	
data3$ordterm[is.na(data3$ordterm)] <- 0

table(data3$ordterm, useNA = "always")
data3$ordterm <- ifelse(data3$ordterm > 0, 1, 0)

################################

#change NA's to 0's	
data3$chfterm[is.na(data3$chfterm)] <- 0

table(data3$chfterm, useNA = "always")
data3$chfterm <- ifelse(data3$chfterm > 0, 1, 0)

################################

#change NA's to 0's	
data3$uhseats[is.na(data3$uhseats)] <- 0

table(data3$uhseats, useNA = "always")
data3$uhseats <- ifelse(data3$uhseats > 0, 1, 0)

################################

#change NA's to 0's	
data3$lhseats[is.na(data3$lhseats)] <- 0

table(data3$lhseats, useNA = "always")
data3$lhseats <- ifelse(data3$lhseats > 0, 1, 0)

################################

#change NA's to 0's	
data3$agterm[is.na(data3$agterm)] <- 0

table(data3$agterm, useNA = "always")
x <- dummy(data3$agterm)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$agterm <- NULL

################################

#change NA's to 0's	
data3$hosterm[is.na(data3$hosterm)] <- 0

table(data3$hosterm, useNA = "always")
x <- dummy(data3$hosterm)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4,5)])
data3$hosterm <- NULL

################################

#change NA's to 0's	
# data3$docs[is.na(data3$docs)] <- 0
# 
# table(data3$docs, useNA = "always")
# x <- dummy(data3$docs)
# x <- as.data.frame(x)
# x
# data3 <- data.frame(data3, x[,c(1,2)])
# data3$docs <- NULL

################################

#change NA's to 0's	
data3$doctit[is.na(data3$doctit)] <- 0

table(data3$doctit, useNA = "always")
x <- dummy(data3$doctit)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$doctit <- NULL

################################

#change NA's to 0's	
data3$model[is.na(data3$model)] <- 0

table(data3$model, useNA = "always")
x <- dummy(data3$model)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$model <- NULL

################################

#change NA's to 0's	
data3$rightsw[is.na(data3$rightsw)] <- 0

table(data3$rightsw, useNA = "always")
x <- dummy(data3$rightsw)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1,2)])
data3$rightsw <- NULL

################################

#change NA's to 0's	
data3$lhelsys[is.na(data3$lhelsys)] <- 4

table(data3$lhelsys, useNA = "always")
x <- dummy(data3$lhelsys)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$lhelsys <- NULL

################################

table(data3$hogterml, useNA = "always")
# x <- dummy(data3$hogterml)
# x
# x <- as.data.frame(x)
# data3 <- data.frame(data3, x[,c(1)])
# names(data3)[ncol(data3)] <- names(x)[1]
data3$hogterml <- NULL

################################

table(data3$colrule, useNA = "always")
# x <- dummy(data3$colrule)
# x
# x <- as.data.frame(x)
# data3 <- data.frame(data3, x[,c(1,2)])
# data3$colrule <- NULL

################################

#change NA's to 0's	
data3$prevlead[is.na(data3$prevlead)] <- 3

table(data3$prevlead, useNA = "always")
x <- dummy(data3$prevlead)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$prevlead <- NULL

################################

#change NA's to 0's	
# data3$forinves[is.na(data3$forinves)] <- 4
# 
# table(data3$forinves, useNA = "always")
# x <- dummy(data3$forinves)
# x
# x <- as.data.frame(x)
# data3 <- data.frame(data3, x[,c(1,2,3)])
# data3$forinves <- NULL

################################

#no variation
data3$amndapct <- NULL

################################

table(data3$suptermn, useNA = "always")
x <- dummy(data3$suptermn)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$suptermn <- NULL

################################

#change NA's to 0's	
data3$execnum[is.na(data3$execnum)] <- 0

table(data3$execnum, useNA = "always")
x <- dummy(data3$execnum)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1, 2, 3)])
data3$execnum <- NULL

################################

#change NA's to 0's	
data3$hoshog[is.na(data3$hoshog)] <- 0

table(data3$hoshog, useNA = "always")
x <- dummy(data3$hoshog)
x
x <- as.data.frame(x)
data3 <- data.frame(data3, x[,c(1,2)])
data3$hoshog <- NULL

################################

#change NA's to 0's	
data3$hoselect[is.na(data3$hoselect)] <- 0

table(data3$hoselect, useNA = "always")
x <- dummy(data3$hoselect)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$hoselect <- NULL

################################

table(data3$hoselsys, useNA = "always")
x <- dummy(data3$hoselsys)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$hoselsys <- NULL

################################

table(data3$hosterml, useNA = "always")
x <- dummy(data3$hosterml)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$hosterml <- NULL

################################

summary(data3$hosage, useNA = "always")
data3$hosage <- ifelse(data3$hosage > 65, NA, data3$hosage)
data3$hosage <- ifelse(data3$hosage == 1, NA, data3$hosage)
data3$hosage <- ifelse(data3$hosage < median(data3$hosage, na.rm = T), 1, 0)

################################

table(data3$hossucc, useNA = "always")
x <- dummy(data3$hossucc)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$hossucc <- NULL

################################

#no variation
data3$hosimm <- NULL

################################

table(data3$hosdecim, useNA = "always")
x <- dummy(data3$hosdecim)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]

data3$hosdecim <- NULL

################################

#no variation
data3$hosdecex <- NULL

################################


data3$hogname <- NULL

################################


data3$hogelect <- NULL

################################

#no variation 
data3$hogage <- NULL

################################

data3$hoglegr <- NULL

################################


data3$hogsucc <- NULL

################################

#no variation
data3$hogimm <- NULL

################################

#no variation
data3$hogdecm <- NULL

################################

#no variation
data3$hogdecex <- NULL

################################

table(data3$depsel, useNA = "always")
x <- dummy(data3$depsel)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$depsel <- NULL

################################

table(data3$cabcoll, useNA = "always")
# x <- dummy(data3$cabcoll)
# x <- as.data.frame(x)
# x
# data3 <- data.frame(data3, x[,c(1)])
# names(data3)[ncol(data3)] <- names(x)[1]
data3$cabcoll <- NULL

################################

#no variation
table(data3$cabage, useNA = "always")
data3$cabage <- NULL

################################

table(data3$cabrestl, useNA = "always")
data3$cabrestl <- NULL

################################

table(data3$atgen, useNA = "always")
x <- dummy(data3$atgen)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$atgen <- NULL

################################

table(data3$agterml, useNA = "always")
x <- dummy(data3$agterml)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$agterml <- NULL

################################

#no variation
table(data3$agage, useNA = "always")
x <- dummy(data3$agage)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$agage <- NULL

################################

table(data3$emdecl, useNA = "always")
x <- dummy(data3$emdecl)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$emdecl <- NULL

################################

table(data3$housenum, useNA = "always")
x <- dummy(data3$housenum)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$housenum <- NULL

################################

table(data3$legjoint, useNA = "always")
x <- dummy(data3$legjoint)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$legjoint <- NULL

################################

table(data3$lhlead, useNA = "always")
x <- dummy(data3$lhlead)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$lhlead <- NULL

################################

table(data3$lhcohort, useNA = "always")
x <- dummy(data3$lhcohort)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$lhcohort <- NULL

################################

summary(data3$lhage, useNA = "always")
data3$lhage <- ifelse(data3$lhage > 65, NA, data3$lhage)
data3$lhage <- ifelse(data3$lhage == 1, 18, data3$lhage)
data3$lhage <- ifelse(data3$lhage < median(data3$lhage, na.rm = T), 1, 0)

################################

summary(data3$lhterm, useNA = "always")
data3$lhterm2 <- ifelse(data3$lhterm > 7, NA, data3$lhterm)
data3$lhterm2 <- ifelse(data3$lhterm2 < median(data3$lhterm2, na.rm = T), 1, 0)
data3$lhterm <- NULL

################################

table(data3$lhtrmlim, useNA = "always")
x <- dummy(data3$lhtrmlim)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$lhtrmlim <- NULL

################################

table(data3$uhlead, useNA = "always")
x <- dummy(data3$uhlead)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$uhlead <- NULL

################################

table(data3$uhcohort, useNA = "always")
x <- dummy(data3$uhcohort)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$uhcohort <- NULL

################################

table(data3$uhelsys, useNA = "always")
x <- dummy(data3$uhelsys)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$uhelsys <- NULL

################################

table(data3$uhage)
data3$uhage <- ifelse(data3$uhage == 1, NA, data3$uhage)
data3$uhage <- ifelse(data3$uhage < median(data3$uhage, na.rm = T), 1, 0)

################################

table(data3$uhtrmlim, useNA = "always")
x <- dummy(data3$uhtrmlim)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$uhtrmlim <- NULL

################################

table(data3$legdiss, useNA = "always")
x <- dummy(data3$legdiss)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$legdiss <- NULL

################################

table(data3$legrep, useNA = "always")
x <- dummy(data3$legrep)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$legrep <- NULL

################################

table(data3$immunity, useNA = "always")
x <- dummy(data3$immunity)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$immunity <- NULL

################################

table(data3$intexec, useNA = "always")
x <- dummy(data3$intexec)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$intexec <- NULL

################################

table(data3$legapp, useNA = "always")
x <- dummy(data3$legapp)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$legapp <- NULL

################################

table(data3$legappdf, useNA = "always")
x <- dummy(data3$legappdf)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$legappdf <- NULL

################################

table(data3$legapppt, useNA = "always")
x <- dummy(data3$legapppt)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$legapppt <- NULL

################################

table(data3$overwho, useNA = "always")
x <- dummy(data3$overwho)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$overwho <- NULL

################################

table(data3$overpct, useNA = "always")
x <- dummy(data3$overpct)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$overpct <- NULL

################################

table(data3$attend, useNA = "always")
x <- dummy(data3$attend)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$attend <- NULL

################################

table(data3$profleg, useNA = "always")
x <- dummy(data3$profleg)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4,5)])
data3$profleg <- NULL

################################

table(data3$recvote, useNA = "always")
x <- dummy(data3$recvote)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$recvote <- NULL

################################

table(data3$levjud, useNA = "always")
x <- dummy(data3$levjud)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$levjud <- NULL

################################

table(data3$chftrmnm, useNA = "always")
x <- dummy(data3$chftrmnm)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$chftrmnm <- NULL

################################

#No Variation
data3$chfage <- NULL

################################

summary(data3$supterm, useNA = "always")
data3$supterm1 <- ifelse(data3$supterm >= median(data3$supterm, na.rm = T), 1, 0)
data3$supterm2 <- ifelse(data3$supterm < median(data3$supterm, na.rm = T), 1, 0)
data3$supterm3 <- ifelse(data3$supterm == 88, 1, 0)
data3$supterm4 <- ifelse(data3$supterm == 89, 1, 0)
data3$supterm5 <- ifelse(data3$supterm == 90, 1, 0)
data3$supterm <- NULL

################################

summary(data3$supage, useNA = "always")
data3$supage <- ifelse(data3$supage == 1 | data3$supage >= 90, NA, data3$supage)
data3$supage <- ifelse(data3$supage < median(data3$supage, na.rm = T), 1, 0)

################################

table(data3$ordterml, useNA = "always")
x <- dummy(data3$ordterml)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$ordterml <- NULL

################################

summary(data3$ordage, useNA = "always")
data3$ordage <- ifelse(data3$ordage == 1 | data3$ordage >= 90, NA, data3$ordage)
data3$ordage <- ifelse(data3$ordage < median(data3$ordage, na.rm = T), 1, 0)

################################

#no variation
data3$adtermn <- NULL

################################

summary(data3$adage, useNA = "always")
data3$adage <- ifelse(data3$adage == 1 | data3$adage >= 90, NA, data3$ordage)
data3$adage <- ifelse(data3$adage < median(data3$adage, na.rm = T), 1, 0)

################################

#no variation
data3$conlim <- NULL

################################

summary(data3$conage, useNA = "always")
data3$conage <- ifelse(data3$conage == 1 | data3$conage >= 90, NA, data3$conage)
data3$conage <- ifelse(data3$conage < median(data3$conage, na.rm = T), 1, 0)


################################

table(data3$jrem, useNA = "always")
x <- dummy(data3$jrem)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$jrem <- NULL

################################

table(data3$judretir, useNA = "always")
x <- dummy(data3$judretir)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$judretir <- NULL

################################

table(data3$fedunit, useNA = "always")
data3$fedunit <- NULL

################################

table(data3$fedsep, useNA = "always")
x <- dummy(data3$fedsep)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$fedsep <- NULL

################################

table(data3$seccess, useNA = "always")
x <- dummy(data3$seccess)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$seccess <- NULL

################################

table(data3$partprf, useNA = "always")
data3$partprf <- NULL

################################

table(data3$partprh, useNA = "always")
data3$partprh <- NULL

################################

table(data3$votemin, useNA = "always")
x <- dummy(data3$votemin)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$votemin <- NULL

################################

table(data3$oversght, useNA = "always")
x <- dummy(data3$oversght)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$oversght <- NULL

################################

#no variation
data3$ecterml <- NULL

################################

#no variaton
data3$ecage <- NULL

################################

table(data3$electsch, useNA = "always")
x <- dummy(data3$electsch)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$electsch <- NULL

################################

table(data3$electsam, useNA = "always")
x <- dummy(data3$electsam)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4,5)])
data3$electsam <- NULL

################################

table(data3$census, useNA = "always")
x <- dummy(data3$census)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$census <- NULL

################################

#no variation
data3$bankgoal <- NULL

################################

table(data3$custlaw2, useNA = "always")
data3$custlaw2 <- NULL

################################

table(data3$headforn, useNA = "always")
data3$headforn <- NULL

################################

table(data3$warap, useNA = "always")
x <- dummy(data3$warap)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$warap <- NULL

################################

table(data3$treatst, useNA = "always")
data3$treatst <- NULL

################################

table(data3$excrim, useNA = "always")
x <- dummy(data3$excrim)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$excrim <- NULL

################################

table(data3$cappun, useNA = "always")
x <- dummy(data3$cappun)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$cappun <- NULL

################################

table(data3$corppun, useNA = "always")
x <- dummy(data3$corppun)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$corppun <- NULL

################################

table(data3$examwit, useNA = "always")
x <- dummy(data3$examwit)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$examwit <- NULL

################################

data3$nat[is.na(data3$nat)] <- 5
table(data3$nat, useNA = "always")
x <- dummy(data3$nat)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$nat <- NULL

################################

#no variation
table(data3$resenex, useNA = "always")
x <- dummy(data3$resenex)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$resenex <- NULL

################################

table(data3$infoaccw, useNA = "always")
data3$infoaccw <- NULL

################################

table(data3$offrel, useNA = "always")
x <- dummy(data3$offrel)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$offrel <- NULL

################################

table(data3$offrelw, useNA = "always")
x <- dummy(data3$offrelw)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$offrelw <- NULL

################################

table(data3$exprcomp, useNA = "always")
x <- dummy(data3$exprcomp)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$exprcomp <- NULL

################################

table(data3$jointrde, useNA = "always")
data3$jointrde <- NULL

################################

table(data3$proprght, useNA = "always")
x <- dummy(data3$proprght)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$proprght <- NULL

################################

#no variation
data3$safework <- NULL

################################

#no variation
data3$childwrk <- NULL

################################

table(data3$marriage, useNA = "always")
x <- dummy(data3$marriage)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$marriage <- NULL

################################

table(data3$uhterm, useNA = "always")
x <- dummy(data3$uhterm)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4,5,6,7)])
data3$uhterm <- NULL

################################

table(data3$amndampct, useNA = "always")
x <- dummy(data3$amndampct)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$amndampct <- NULL

################################

table(data3$legsupr, useNA = "always")
x <- dummy(data3$legsupr)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$legsupr <- NULL

################################

table(data3$slave, useNA = "always")
x <- dummy(data3$slave)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3,4)])
data3$slave <- NULL

################################

table(data3$torture, useNA = "always")
x <- dummy(data3$torture)
x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$torture <- NULL

################################

table(data3$censor, useNA = "always")
data3$censor <- NULL

################################

table(data3$press, useNA = "always")
x <- dummy(data3$press)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$press <- NULL

################################

table(data3$nomil, useNA = "always")
x <- dummy(data3$nomil)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2,3)])
data3$nomil <- NULL

################################

table(data3$govmed, useNA = "always")
data3$govmed <- NULL

################################

table(data3$lang, useNA = "always")
x <- dummy(data3$lang)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$lang <- NULL

################################

table(data3$langprot, useNA = "always")
data3$langprot <- NULL

################################

#no variation
data3$edcompl <- NULL

################################

table(data3$edfreel, useNA = "always")
x <- dummy(data3$edfreel)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1)])
names(data3)[ncol(data3)] <- names(x)[1]
data3$edfreel <- NULL

################################

table(data3$achighed, useNA = "always")
x <- dummy(data3$achighed)

x <- as.data.frame(x)
x
data3 <- data.frame(data3, x[,c(1,2)])
data3$achighed <- NULL

data3$X <- NULL
data3[is.na(data3)] <- 0
data3 <- as.data.frame(data3)

#drop unanimous columns 
sds <- apply(data3, 2, function(x) sd(x))
cols <- names(sds[sds == 0])
data3 <- data3[,names(data3) %in% cols == FALSE]

write.csv(data3, "roll_call_matrix.csv")
